package com.sevenre.trackre.driver;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.sevenre.trackre.driver.datatypes.TaggingStop;
import com.sevenre.trackre.driver.datatypes.TaggingStopAdapter;
import com.sevenre.trackre.driver.network.Server;
import com.sevenre.trackre.driver.utils.CancelTripDialogBox;
import com.sevenre.trackre.driver.utils.CompleteDialogBox;
import com.sevenre.trackre.driver.utils.ControlTripDialogBox;

public class ActivityTagging extends Activity implements OnClickListener{
	
	ArrayList<TaggingStop> result;
	ListView lv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tagging);
		setUpUI();
		new GetTaggingStop().execute();
	}

	private void setUpUI() {
		lv = (ListView)findViewById(R.id.tagging_list_view);
		getActionBar().setBackgroundDrawable(new ColorDrawable(Color.rgb(243,187,90)));
		((ImageButton)findViewById(R.id.tagging_add_new_stop)).setEnabled(false);
		/*Calendar c = Calendar.getInstance();
		((EditText)findViewById(R.id.edit_text_add_stop_hr)).setText(c.get(Calendar.HOUR));
		((EditText)findViewById(R.id.edit_text_add_stop_min)).setText(c.get(Calendar.MINUTE));
		if (c.get(Calendar.HOUR_OF_DAY)>12){
			((Button)findViewById(R.id.button_add_stop_am_pm)).setText("PM");
		} else {
			((Button)findViewById(R.id.button_add_stop_am_pm)).setText("AM");
		}*/
		EditText et = (EditText)findViewById(R.id.edit_text_add_stop_name);
		et.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (s.length() > 3){
					((ImageButton)findViewById(R.id.tagging_add_new_stop)).setEnabled(true);
				} else {
					((ImageButton)findViewById(R.id.tagging_add_new_stop)).setEnabled(false);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
					
			}
		});
	}

	@Override
	public void onClick(View v) {
		
	}
	
	public void taggingOnClick(View v) {
		switch (v.getId()){
		case R.id.tagging_button_cancel:
			CancelTripDialogBox d = new CancelTripDialogBox(this, "Are you sure you want to Cancel trip?");
			d.show();
			break;
		case R.id.tagging_button_completed:
			CompleteDialogBox d1 = new CompleteDialogBox(this, "Are you sure you want to Complete trip?");
			d1.show();
			break;
		case R.id.tagging_button_add:
			findViewById(R.id.taggin_ll_add_details).setVisibility(View.VISIBLE);
			findViewById(R.id.tagging_ll_controls).setVisibility(View.GONE);
			Calendar c = Calendar.getInstance();
			((EditText)findViewById(R.id.edit_text_add_stop_hr)).setText(""+c.get(Calendar.HOUR));
			((EditText)findViewById(R.id.edit_text_add_stop_min)).setText(""+c.get(Calendar.MINUTE));
			if (c.get(Calendar.HOUR_OF_DAY)>12){
				((Button)findViewById(R.id.button_add_stop_am_pm)).setText("PM");
			} else {
				((Button)findViewById(R.id.button_add_stop_am_pm)).setText("AM");
			}
			break;
		case R.id.tagging_add_new_stop_cancel:
			findViewById(R.id.tagging_ll_controls).setVisibility(View.VISIBLE);
			findViewById(R.id.taggin_ll_add_details).setVisibility(View.GONE);
			hideKeyboard();
			break;
		case R.id.tagging_add_new_stop:
			findViewById(R.id.tagging_ll_controls).setVisibility(View.VISIBLE);
			findViewById(R.id.taggin_ll_add_details).setVisibility(View.GONE);
			hideKeyboard();
			break;
		}
	}
	
	void hideKeyboard(){
		InputMethodManager inputMethodManager = (InputMethodManager)  getSystemService(Activity.INPUT_METHOD_SERVICE);
	    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
	}
	
	private void updateListView(ArrayList<TaggingStop> result) {
		this.result = result;
		TaggingStopAdapter adapter = new TaggingStopAdapter(this, result, getResources());
		lv.setAdapter(adapter);
		int[] colors = { 0, R.color.violet_3, 0 }; // red for the example
		lv.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
		lv.setDividerHeight(2);
		lv.setOnItemClickListener(adapter);
	}
	
	class GetTaggingStop extends AsyncTask<String, Integer, ArrayList<TaggingStop>> {

		@Override
		protected ArrayList<TaggingStop> doInBackground(String... arg0) {
			return Server.getTaggingStopList();
		}

		@Override
		protected void onPostExecute(ArrayList<TaggingStop> result) {
			updateListView(result);
			super.onPostExecute(result);
		}
	}
	
	
}
